package com.bangdream.michelia.view.fragment.main.live;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.LiveContract;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.entity.LiveUrlBean;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.presenter.LivePersenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.live.LiveDetails;
import com.bangdream.michelia.view.adapter.LiveAdapter;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @date on 2018/6/20 0:41
 * @email 909732184@qq.com
 * 首页-直播
 */
public class MainLive extends LazyLoadFragment<LiveContract.ILiveView,LivePersenter<LiveContract.ILiveView>>
implements LiveContract.ILiveView{
    private View view;
    private View statusBarLayout;
    private XRefreshView refreshView;
    private LinearLayoutManager linearLayoutManager;
    private RecyclerView recyclerView;

    LiveAdapter adapter;
    private int page = 1;
    private List<LiveBean> allData;
    private List<LiveBean> tempData = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_item_live,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
        operateBus();
    }

    private void setListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(getActivity(),LiveDetails.class);
                intent.putExtra("id",adapter.getData().get(position).getId());
                intent.putExtra("count",adapter.getData().get(position).getLivePersonCnt());
                intent.putExtra("name",adapter.getData().get(position).getName());

                startActivity(intent);
            }
        });


        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                getData();
            }
        });
    }

    @Override
    public LivePersenter createPresenter() {
        return new LivePersenter();
    }

    @Override
    public void loadData() {
        getData();
    }

    private void initView() {
        initTitleBar("","直播","",null);
        statusBarLayout = view.findViewById(R.id.statusBarLayout);
        ViewGroup.LayoutParams layoutParams = statusBarLayout.getLayoutParams();
        layoutParams.height = MainActivity.statusBarHeight;
        statusBarLayout.setLayoutParams(layoutParams);
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        recyclerView = view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new LiveAdapter(R.layout.live_adapter_item);
        recyclerView.setAdapter(adapter);
        initEmptyView(recyclerView);
    }

    @Override
    public void setLiveList(boolean isSuccess, int page, List<LiveBean> data, String stDescribe) {
        refreshView.stopRefresh();
        refreshView.stopLoadMore();
        if (isSuccess && data!=null){
            if(page==1) {
                if(data.size()==0){
                    adapter.setEmptyView(noDataView);
                }else{
                    adapter.setNewData(data);
                    adapter.notifyDataSetChanged();
                    //显示数据大于20条才允许上拉加载更多
                    if(data.size()>=PublicResources.PageSize){
                        refreshView.setPullLoadEnable(true);
                    }
                }
            }else {
                int tempIndex = adapter.getData().size();
                for (int i = 0;i<data.size();i++) {
                    adapter.getData().add(data.get(i));
                }
                adapter.notifyItemChanged(tempIndex,adapter.getData().size());
                refreshView.stopLoadMore();
            }
            allData = adapter.getData();
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                refreshView.stopLoadMore();
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                adapter.setNewData(null);
                refreshView.stopRefresh();
                adapter.setEmptyView(noDataView);

            }
        }
    }

    @Override
    public void setLiveDetails(boolean isSuccess, LiveBean data, String stDescribe) {

    }

    @Override
    public void setLiveUrl(boolean isSuccess, List<String> url) {

    }

    @Override
    public void setRePlayLiveUrl(boolean isSuccess, List<String> url) {

    }

    public void getData() {
        adapter.setEmptyView(loadingView);
        mPresenter.getLiveListData(page, PublicResources.PageSize
                ,null,null);
    }

    public void searchList(String stText){
        tempData.clear();
        if(stText!=null&&stText.length()>0){
            if(allData==null)
                return;
            for(int i = 0;i<allData.size();i++){
                if(allData.get(i).getName().contains(stText)){
                    tempData.add(allData.get(i));
                }
            }
            adapter.setNewData(tempData);
        }else {
            adapter.setNewData(allData);
        }
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("CheckResult")
    public void operateBus() {
        disposable = RxBus.getInstance().toObservable().map(o -> (RxEventBean) o).subscribe(bean -> {
            if(bean.getCode()==0x02){
                page=1;
                refreshView.startRefresh();
            }
        });
    }
}
